package com.bk.controller;

import java.util.List;

import com.cjw.model.DBMap;
import com.moomi.common.model.BkShop;
import com.moomi.common.model.BkUser;
import com.moomi.common.model.BkUserShop;

/**
 * 分店管理
 * 
 * @author cen
 *
 */
public class BKBranchOfficeController extends BKAuthController {

	@Override
	public void index() {
		super.index();
		setTitle("分店管理");
		int userId = getUserId();
		List<BkShop> shops = BkShop.dao.userShops(userId);
		// List<BkBranchOffice> items = BkBranchOffice.dao.search("shopId",
		// getShopId());
		setAttr("items", shops);
		render("/bk/shop/table.html");
	}

	// @Override
	// public void info() {
	// super.info();
	// BkBranchOffice info = BkBranchOffice.dao.findById(getId());
	// setInfoItem(info);
	// ArrayList<Object> array = new ArrayList<>();
	// TimeUtils timeUtil = new TimeUtils(2);
	// for (int i = 0; i < 24; i++) {
	// String time = timeUtil.timeIndexToString(i * 2);
	// HashMap<String, Object> map = new HashMap<>();
	// map.put("str", time);
	// map.put("index", i);
	// array.add(map);
	// System.out.println(time);
	// }
	// setAttr("times", array);
	// render("admin-form.html");
	// }

	@Override
	public void update() {
		super.update();
		BkShop shop = (BkShop) updateModel(BkShop.class);
		int shopId = shop.getId();
		int userId = getUserId();
		DBMap map = new DBMap();
		map.put("shopId", shopId);
		map.put("userId", userId);
		List<BkUserShop> userShops = BkUserShop.dao.search(map);
		if (userShops.size() > 0) {
			// BkUserShop userShop = userShops.get(0);
		} else {
			BkUserShop userShop = new BkUserShop();
			userShop.setUserId(userId);
			userShop.setShopId(shopId);
			userShop.save();
		}
	}

	@Override
	public void delete() {
		// deleteModel(BkShop.class);
		int id = getParaToInt("id");
		boolean flag = BkShop.dao.deleteById(id);
		if (flag) {
			renderSuccess();
		} else {
			renderMsg("删除失败");
		}
	}

	public void manageItem() {

	}

	public void manageSelection() {
	}

	public void swithShop() {
		Integer shopId = getParaToInt("shopId");
		Integer button = getParaToInt("button");
		setShopId(shopId);
		int newShopId = getShopId();
		// int port = getRequest().getLocalPort();
		// String hostString = getRemoteHost();
		// String host = "http://" + hostString + ":" + port + "/bk/item/";
		// renderText(host);
		BkUser user = getUser();
		user.setSelectedShop(newShopId);
		user.update();
		String url = "/bk/item/";
		redirect(url);
		if (button != null) {
			renderHref(url);
		}
	}
}
